<?php

namespace Home\Controller;

class UserController extends UserCommonController
{
    public function index()
    {
        $this->assign('nav_select', 'index');
        $uid = session('ytata.uid');
        $this->assign('uid', $uid);
        $map['uid'] = $uid;
        $sort       = ' id DESC ';
        $list       = D('Article')->get_list($map, false, $sort, 3);
        $this->assign('new', $list);
        $this->display();
    }

    //系统消息
    public function msg()
    {
        $read          = I('get.read');
        $uid           = session('ytata.uid');
        $map['to_uid'] = $uid;
        if ($read <> '') {
            $map['read'] = intval($read);
        }
        $msglist = D('UserMsg')->get_list($map, true, 10);
        $this->assign('msg', $msglist);
        $this->assign('read', $read);
        $this->assign('nav_select', 'msg');
        $this->display();
    }

    //消息详情
    public function msg_view()
    {
        $id   = I('get.id', '0', 'intval');
        $uid  = session('ytata.uid');
        $show = D('UserMsg')->get_user_msg_one($id);
        D('UserMsg')->set_msg_read($id, $uid, 1);
        $this->assign('show', $show);
        $this->assign('nav_select', 'msg');
        $this->display();
    }

    //删除系统消息
    public function msg_del()
    {
        $ids = I('request.id');
        $uid = session('ytata.uid');
        $ids = is_array($ids) ? implode(",", $ids) : intval($ids);
        if ($ids && preg_match("/^(\d{1,10},)*(\d{1,10})$/", $ids)) {
            $map['id']     = array('IN', "{$ids}");
            $map['to_uid'] = $uid;
            // 更新显示状态
            $s = D('UserMsg')->where($map)->setField('is_show', 0);
            $this->success("成功删除{$s}条数据");
        } else {
            $this->error('请选择要删除的信息！');
        }
    }

    //私信
    public function pms()
    {
        $read                   = I('get.read');
        $uid                    = session('ytata.uid');
        $map['send_uid|to_uid'] = $uid;
        if ($read <> '') {
            $is_read        = intval($read);
            $map['_string'] = "(send_uid='{$uid}' and send_read='{$is_read}') or (to_uid='{$uid}' and to_read='{$is_read}')";
            $whereand       = ' AND ';
        }
        $map['_string'] = $map['_string'] . $whereand . " ((send_uid='{$uid}' and send_display='1') or (to_uid='{$uid}' and to_display='1'))";
        //$map['_string'] = "(uid='{$uid}' AND )";
        //print_r($map);
        $pms = D('UserPms')->get_list($map, true, 10, $uid);
        //dump(D('UserPms')->getlastSql());
        $this->assign('pms', $pms);
        $this->assign('uid', $uid);
        $this->assign('read', $read);
        $this->assign('nav_select', 'msg');
        $this->display();
    }

    //私信详情
    public function pms_view()
    {
        $id   = I('get.id', '0', 'intval');
        $uid  = session('ytata.uid');
        $list = D('UserPmsChat')->get_list($id, $uid);
        D('UserPms')->set_pms_read($id, $uid, 1);
        //echo D('UserPms')->getlastSql();
        $this->assign('list', $list);
        $this->assign('uid', $uid);
        $this->assign('nav_select', 'msg');
        $this->display();
    }

    //删除私信
    public function pms_del()
    {
        $id  = I('request.id', '0', 'intval');
        $uid = session('ytata.uid');

        if (empty($id)) {
            $this->error('请选择要删除的信息！');
        }

        $pms = D('UserPms')->get_pms_one($id);
        if ($pms['send_uid'] == $uid) {
            $data['id']           = $id;
            $data['send_display'] = 0;
            D('UserPms')->save($data);
            $chatwhere['pid']         = $id;
            $chatdata['send_display'] = 0;
            M('UserPmsChat')->where($chatwhere)->save($chatdata);
        } else {
            $data['id']         = $id;
            $data['to_display'] = 0;
            D('UserPms')->save($data);
            $chatwhere['pid']       = $id;
            $chatdata['to_display'] = 0;
            M('UserPmsChat')->where($chatwhere)->save($chatdata);
        }
        $this->success("成功删除");
    }

    //基本资料
    public function set_basic()
    {
        $this->assign('nav_select', 'set');
        $oauth_list = ytata_get_cache('oauth');
        foreach ($oauth_list as $key => $val) {
            $val['is_bind'] = 0;
            $map['uid']     = session('ytata.uid');
            $map['type']    = $val['alias'];
            $info           = M('UserBind')->where($map)->find();
            if (!empty($info)) {
                $val['is_bind'] = 1;
            }
            $oauth[$key] = $val;
        }
        $this->assign('oauth_list', $oauth);
        $this->display();
    }

    //头像
    public function set_avatar()
    {
        $this->assign('nav_select', 'set');
        $this->display();
    }

    //设置密码
    public function set_pwd()
    {
        $this->assign('nav_select', 'set');
        $this->display();
    }

    //积分记录
    public function set_gold()
    {
        $this->assign('nav_select', 'set');
        $map['uid']  = session('ytata.uid');
        $map['type'] = 1;
        $this->assign('list', D('LogGold')->log_list($map));
        $this->display();
    }

    //经验记录
    public function set_exp()
    {
        $this->assign('nav_select', 'set');
        $map['uid']  = session('ytata.uid');
        $map['type'] = 2;
        $this->assign('list', D('LogGold')->log_list($map));
        $this->display();
    }

    //我的等级
    public function set_lv()
    {
        $this->assign('nav_select', 'set');
        $Task = D("Lv");
        $list = $Task->get_lv_list();
        $this->assign('list', $list);
        $this->display();
    }

    //登录日志
    public function set_loginlog()
    {
        $uid = session('ytata.uid');
        $this->assign('nav_select', 'set');
        $this->assign('list', D('LogLogin')->log_list($uid));
        $this->display();
    }

    //我的任务
    public function task()
    {
        $type = I('get.type', '', 'intval');
        $this->assign('type', $type);
        $uid = session('ytata.uid');
        if ($type > 0) {
            $map['type'] = intval($type);
        }
        $map['is_display'] = 1;
        $list              = D('Task')->get_list($map, true, $uid);
        $this->assign('list', $list);
        $this->assign('nav_select', 'task');
        $this->display();
    }

    //我的分享(文章)
    public function article()
    {
        $this->assign('nav_select', 'article');
        $exc = I('get.exc');
        $this->assign('exc', $exc);
        if ($exc <> '') {
            $map['excellent'] = intval($exc);
        }
        $map['uid'] = session('ytata.uid');
        $sort       = ' id DESC ';
        $list       = D('Article')->get_list($map, true, $sort, 10);
        $this->assign('new', $list);
        $this->display();
    }

    //删除文章
    public function article_del()
    {
        $uid = session('ytata.uid');
        $id  = I('get.id');
        //获取标签
        $tagmap['qid'] = $id;
        $tags          = M('ArticleTag')->where($tagmap)->select();
        foreach ($tags as $t) {
            $set_tag[] = $t['tagid'];
        }
        D('Article')->del_article($id, $uid);
        //更新标签统计
        D('Tag')->set_tag_count($set_tag, 'count_article');
        $this->success('删除成功');
    }

    //修改我的分享(文章)
    public function article_edit()
    {
        $this->assign('nav_select', 'article');
        $id              = I('get.id');
        $show            = D('Article')->get_one_id($id);
        $show['img_url'] = D('Article')->get_article_img($show['img'], $id);
        $this->assign('show', $show);
        $this->display();
    }

    //修改我的分享(文章)保存
    public function article_edit_save()
    {
        $data = D("Article");
        $uid  = session('ytata.uid');
        if (!$data->create()) {
            $this->error($data->getError());
        } else {
            $data->img = I('post.img', '', 'trim');
            $data->save();
            $aid = I('post.id', '', 'intval');
            $tag = I('post.tag');
            if (!empty($tag))//插入标签
            {
                //先清空以前的标签
                $tags = D('ArticleTag')->get_tag_qid($aid);
                M('ArticleTag')->where('aid=' . $aid)->delete();
                foreach ($tags as $tv) {
                    $del_tag[] = $tv['tagid'];
                }
                //更新标签统计
                D('Tag')->set_tag_count($del_tag, 'count_article');
                //
                if (is_array($tag)) {
                    foreach ($tag as $v) {
                        $map['aid'] = $aid;
                        $map['uid'] = $uid;
                        $tag        = D('Tag')->get_one_tag($v);
                        if (empty($tag)) {
                            $new_tag['tag']     = $v;
                            $new_tag['addtime'] = time();
                            $tagid              = M('Tag')->add($new_tag);
                            $map['tagid']       = $tagid;
                        } else {
                            $map['tagid'] = $tag['id'];
                        }
                        $map['tag']  = $tag['tag'];
                        $count_tag[] = $map['tagid'];
                        M('ArticleTag')->add($map);
                    }
                    //更新标签统计
                    D('Tag')->set_tag_count($count_tag, 'count_article');
                }
            }
            $this->success('保存完成');
        }
    }

    //我的问答
    public function question()
    {
        $uid = session('ytata.uid');
        $this->assign('nav_select', 'question');
        $this->assign('mods', I('get.mods', '1', 'intval'));
        $this->assign('type', I('get.type'));
        $this->assign('optimal', I('get.optimal'));
        $this->assign('uid', $uid);
        $this->display();
    }

    //删除问题
    public function question_del()
    {
        $uid = session('ytata.uid');
        $id  = I('get.id', '0', 'intval');
        //获取标签
        $tagmap['qid'] = $id;
        $tags          = M('QuestionTag')->where($tagmap)->select();
        foreach ($tags as $t) {
            $set_tag[] = $t['tagid'];
        }
        D('Question')->del_question($id, $uid);
        //更新标签统计
        D('Tag')->set_tag_count($set_tag, 'count_question');
        $this->success('删除成功');
    }

    //删除问题
    public function bbs_del()
    {
        $uid = session('ytata.uid');
        $id  = I('get.id', '0', 'intval');
        //获取标签
        $tagmap['qid'] = $id;
        $tags          = M('BbsTag')->where($tagmap)->select();
        foreach ($tags as $t) {
            $set_tag[] = $t['tagid'];
        }
        D('Bbs')->del_bbs($id, $uid);
        //更新标签统计
        D('Tag')->set_tag_count($set_tag, 'count_bbs');
        $this->success('删除成功');
    }

    //我的讨论
    public function bbs()
    {
        $uid = session('ytata.uid');
        $this->assign('nav_select', 'bbs');
        $this->assign('mods', I('get.mods', '1', 'intval'));
        $this->assign('type', I('get.type'));
        $this->assign('optimal', I('get.optimal'));
        $this->assign('uid', $uid);
        $this->display();
    }

    //统计
    public function statistics()
    {
        $map['uid']                  = session('ytata.uid');
        $user                        = D('User')->get_user_one($map['uid']);
        $ajax['status']              = 1;
        $ajax['content']             = "统计成功！";
        $ajax['sta']['question']     = M('Question')->where($map)->count();
        $ajax['sta']['answer']       = M('Answer')->where($map)->count();
        $ajax['sta']['acc_rate']     = $user['acc_rate'];
        $ajax['sta']['answer_click'] = D('Answer')->get_click_total($map['uid']);
        $ajax['sta']['count_praise'] = $user['count_praise'];
        $ajax['sta']['gold']         = $user['gold'];
        $ajax['sta']['exps']         = $user['exp'];
        $this->ajaxReturn($ajax);
    }

}